/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 */
package top.hiai.user.index;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import top.hiai.common.model.Article;
import top.hiai.common.model.Reply;
import top.hiai.common.model.User;
import top.hiai.common.util.JsoupFilter;

import java.util.List;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top
* @Introduction 类的介绍
*/

public class IndexUserService {

	public static final IndexUserService me = new IndexUserService();
	
	private User userDao = new User().dao();
	private Article articleDao = new Article().dao();
	private Reply replyDao = new Reply().dao();
	
	/**
	 * 遍历出所有的用户，只遍历正常的用户，即status = 1
	 * 根据用户的威望来排序
	 */
	public Page<User> userListPage(int pageNumber, int pageSize){
		return userDao.paginate(pageNumber, pageSize, "select uid,nickName,avatar,sign,points,prestige,views,is_auth ", " "
				+ " from  bbs_user where bbs_user.status=1 order by bbs_user.prestige DESC,bbs_user.points desc");
	}
	/**
	 * 根绝前台传过来的ID获取用户信息
	 * @param uid
	 * @return
	 */
	public User getDetailUser(int uid){
		return userDao.findFirst(" select uid,nickName,createAt,avatar,sign,points,prestige,views,is_auth,authName,authTime "
				+ " from bbs_user where bbs_user.uid =? ",uid);
	}
	/**
	 * 获取该用户发表的所有文章
	 * 并获取该文章有多少评论数量
	 * @param uid
	 * @return
	 */
	public Page<Article> getUserArticleListPage(int pageNum, int pageSize ,int uid){
        
		//现在我这里不用这种方式，这样浪费数据库的执行时间
		//left join左连接，即使文章中没有评论同时也要输出这个文章
		//select a.aid, a.title ,count(r.rid) from bbs_article  as a left join bbs_reply  as r on a.aid=r.aid group by a.aid ;
		
		String select = " select aid,title,content,addtime,views,favoriteCount,replyCount,status,reason ";
        String from = " from bbs_article where uid=? order by addtime desc ";
        Page<Article> articlePage = articleDao.paginate(pageNum, pageSize, select, from, uid);
		// 列表页显示 content 的摘要信息需要过滤为纯文本，去除所有标记，限制标题输出的长度
		JsoupFilter.filterArticleList(articlePage.getList(), 30, 120);
        return articlePage;
	}
	/**
	 * 获取该用户所有的回复内容
	 * @param uid
	 * @return
	 */
	public List<Reply> getUserReplyList(int uid){
		return replyDao.find("select r.content,r.addtime,a.aid,a.title "
				+ "from bbs_reply as r,bbs_article as a "
				+ "where r.uid=? and r.aid=a.aid and r.status=1 order by r.addtime desc",uid);
	}
	/**
	 * 返回该用户关注了那些人
	 * @param uid
	 * @return
	 */
	public List<User> getUserAttentionList(int uid){
		//select * from bbs_friend as f ,bbs_user as u where f.uid=u.uid and f.fids=1;
		return userDao.find(" select f.fid,f.fids,u.uid,u.nickName,u.sign,u.avatar,u.prestige,u.points "
		//return userDao.findFirst(" select * "
				+ " from bbs_friend as f , bbs_user as u "
				+ " where f.uid = u.uid and f.fids = ?",uid);
	}
	/**
	 * 返回该用户的粉丝
	 * @param uid
	 * @return
	 */
	public List<User> getUserFanList(int uid){
		//select u.uid,u.nickName from bbs_user as u , bbs_friend as f where f.uid = 2 and f.fids = u.uid  ;
		return userDao.find(" select f.fid,f.fids,u.uid,u.nickName,u.sign,u.avatar,u.prestige,u.points "
				+ "from bbs_user as u , bbs_friend as f "
				+ "where f.uid = ? and f.fids = u.uid ", uid);
	}
	/**
	 * 遍历出该用户有多少位粉丝
	 * @param uid
	 * @return
	 */
	public int getUserFanCount(int uid){
		//select count(*) from bbs_user as u , bbs_friend as f where f.uid = 1 and f.fids = u.uid;
		return Db.queryInt(" select count(*) from bbs_user as u , bbs_friend as f "
				+ " where f.uid = ? and f.fids = u.uid ", uid);
	}
	/**
	 * 判断A用户是否关注了B用户
	 * friendRelation = -1	访问者就是A，也就是A用户看了A用户的空间
	 * friendRelation = 0   A没有关注B
	 * friendRelation = 1 	A关注了B
	 * friendRelation = 2 	A与B相互关注了
	 * @return
	 * @param uid B用户的ID，
	 * @param accountId A用户的ID  也就是登录用户的ID
	 */
	public int getFriendRelation(int accountId,int uid){
		if(uid == accountId ){
			return 0;
		}
		List<Record> friendList = Db.find(" select fids,uid from bbs_friend where fids=? and uid=? union all "
				+ " select fids,uid from bbs_friend where fids=? and uid=? ", accountId,uid,uid,accountId);
		// friendList.size() == 0   两个账号没有关系
		if(friendList.size() == 0){
			return 0;				
		}
		
		// friendList.size() == 1  fids账号关注了uid账号
		if(friendList.size() == 1){
			if (friendList.get(0).getInt("fids") == accountId) {
				return 1;               // fids 关注了 uid
			} else {
				return 2;               // uid 也关注了 fids
			}	
		}
		if (friendList.size() == 2) {
			return 3;                   // accountId 与 friendId 互相关注
		}
		throw new RuntimeException("不可能存在的第五种关系，正常情况下该异常永远不可能抛出");
	}
	
    /**
     * 用户浏览别人个人主页，浏览量自动加1
     * 后期要使用别的方法，防止用户恶意刷新
     * @param uid
     */
    public void initUserView(int uid) {
    	Db.update(" update bbs_user set views = views+1 where uid = ? ", uid);
    }
}